#!/usr/bin/env pwsh
param(
    [String]$Prefix
)

# https://www.openssl.org/source/openssl-1.1.1.tar.gz
$OPENSSL_VERSION = "1.1.1g"
#https://curl.haxx.se/download/curl-7.61.1.tar.gz
$NGHTTP2_VERSION = "1.40.0"
$CURL_VERSION = "7.70.0"
#https://curl.haxx.se/ca/cacert-2019-10-16.pem
#https://curl.haxx.se/ca/cacert-2020-01-01.pem
$CACERT_VERSION = "2020-01-01"
# https://c-ares.haxx.se/download/c-ares-1.14.0.tar.gz
$CARES_VERSION = "1.15.0"

$OPENSSL_URL = "https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz"
$OPENSSL_FILE = "openssl-${OPENSSL_VERSION}"
$NGHTTP2_URL = "https://github.com/nghttp2/nghttp2/releases/download/v${NGHTTP2_VERSION}/nghttp2-${NGHTTP2_VERSION}.tar.gz"
$NGHTTP2_FILE = "nghttp2-${NGHTTP2_VERSION}"
$CARES_URL = "https://c-ares.haxx.se/download/c-ares-${CARES_VERSION}.tar.gz"
$CARES_FILE = "c-ares-${CARES_VERSION}"
$CURL_URL = "https://curl.haxx.se/download/curl-${CURL_VERSION}.tar.xz"
$CURL_FILE = "curl-${CURL_VERSION}"
$CA_BUNDLE = "https://curl.haxx.se/ca/cacert-${CACERT_VERSION}.pem"


$BUILD_TEMP = "/tmp/curl_dist/build"

if ([String]::IsNullOrEmpty($Prefix)) {
    $Prefix = "/opt/charlie"
}

if (!(Test-Path $Prefix)) {
    New-Item -ItemType Directory -Force $Prefix
}

New-Item -ItemType Directory "/tmp/curl_dist" -Force
Set-Location "/tmp/curl_dist"
# Powershell core no wget alias, is good
wget $OPENSSL_URL -O "${OPENSSL_FILE}.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}

tar -xvf "${OPENSSL_FILE}.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
Set-Location "/tmp/curl_dist/${OPENSSL_FILE}"


./config "--prefix=${BUILD_TEMP}" no-shared no-unit-test  -fPIC
make
make install_sw

Set-Location "/tmp/curl_dist"
wget $NGHTTP2_URL -O "${NGHTTP2_FILE}.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
tar -xvf "${NGHTTP2_FILE}.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
New-Item -ItemType Directory "/tmp/curl_dist/nghttp2_build" -Force
Set-Location "/tmp/curl_dist/nghttp2_build"

cmake -DCMAKE_BUILD_TYPE=Release "-DCMAKE_INSTALL_PREFIX=${BUILD_TEMP}" `
    "-DCMAKE_PROJECT_nghttp2_INCLUDE=${PSScriptRoot}/nghttp2.cmake" `
    "-DOPENSSL_INCLUDE_DIR=${BUILD_TEMP}/include" `
    "-DOPENSSL_SSL_LIBRARY=${BUILD_TEMP}/lib/libssl.a" `
    "-DOPENSSL_CRYPTO_LIBRARY=${BUILD_TEMP}/lib/libcrypto.a" `
    -DENABLE_STATIC_LIB=ON -DENABLE_LIB_ONLY=ON "/tmp/curl_dist/${NGHTTP2_FILE}"
make
make install
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
Set-Location "/tmp/curl_dist"
wget $CARES_URL -O "$CARES_FILE.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
tar -xvf "${CARES_FILE}.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
New-Item -ItemType Directory "/tmp/curl_dist/c-ares-build" -Force
Set-Location "/tmp/curl_dist/c-ares-build"
cmake -DCMAKE_BUILD_TYPE=Release   -DCARES_BUILD_TOOLS=OFF `
    -DCARES_STATIC=ON -DCARES_STATIC_PIC=ON `
    "-DCMAKE_INSTALL_PREFIX=$BUILD_TEMP" "/tmp/curl_dist/${CARES_FILE}"
make
make install
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}

Set-Location "/tmp/curl_dist"
New-Item  -ItemType Directory  "$Prefix/bin" -Force
$CA_BUNDLE_FILE = "$Prefix/bin/ca-bundle.crt"
wget $CA_BUNDLE -O $CA_BUNDLE_FILE
wget $CURL_URL -O "${CURL_FILE}.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
tar -xvf "${CURL_FILE}.tar.gz"
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}

Set-Location "/tmp/curl_dist/${CURL_FILE}"
Write-Host -ForegroundColor Green "Apply curl link patch"
patch -Nbp1 -i "${PSScriptRoot}/curl.patch"

New-Item -ItemType Directory "/tmp/curl_dist/curl_build"
Set-Location "/tmp/curl_dist/curl_build"
cmake -DCMAKE_BUILD_TYPE=Release `
    "-DCMAKE_INSTALL_PREFIX=${Prefix}" `
    "-DOPENSSL_INCLUDE_DIR=${BUILD_TEMP}/include" `
    "-DOPENSSL_SSL_LIBRARY=${BUILD_TEMP}/lib/libssl.a" `
    "-DOPENSSL_CRYPTO_LIBRARY=${BUILD_TEMP}/lib/libcrypto.a" `
    -DCMAKE_USE_LIBSSH2=OFF `
    -DCURL_DISABLE_LDAP=ON `
    -DUSE_NGHTTP2=ON `
    "-DNGHTTP2_INCLUDE_DIR:STRING=${BUILD_TEMP}/include" `
    "-DNGHTTP2_LIBRARY:STRING=${BUILD_TEMP}/lib/libnghttp2_static.a" `
    -DENABLE_ARES=ON `
    -DBUILD_SHARED_LIBS=OFF `
    -DCURL_STATICLIB=ON `
    "-DCARES_LIBRARY:STRING=${BUILD_TEMP}/lib/libcares_static.a" `
    "-DCARES_INCLUDE_DIR:STRING=${BUILD_TEMP}/include" `
    -DBUILD_TESTING=OFF `
    "-DCURL_CA_BUNDLE=${CA_BUNDLE_FILE}" `
    "/tmp/curl_dist/${CURL_FILE}"
make
make install
if ($LASTEXITCODE -ne 0) {
    # download file error
    exit $LASTEXITCODE
}
